<template>

    <div>
        <div style="margin-bottom: 5px">
            <Button @click="btn_refresh">刷新</Button> 
        </div>
        <i-table border stripe :columns="format" :data="show_list" height="720"></i-table >
        <div style="text-align: center;margin-top: 10px;">
            <Page ref="page" :total="total" :page-size="pageSize"  @on-change="changePage" show-total/>
        </div>
    </div>
</template>

<script>

import { k8s_namespace_post } from '@/api/http'
import { get_event_list } from  '@/api'


export default {
    data() {
        return {
            format: [
                {
                    title: '名字',key: 'name',width: 300,
                },
                {
                    title: '命名空间',key: 'namespace'
                },
                {
                    title: 'last_seen',key: 'last_seen',sortable: true
                },
                {
                    title: '信息',key: 'message',width: 500,
                },
                {
                    title: '原因',key: 'reason',
                },
                {
                    title: '类型',key: 'type',
                },
                {
                    title: '对象',key: 'object',width: 300,
                },
                {
                    title: '源头',key: 'source',
                },
            ],
            total_list: [],
            show_list: [],
            // 分页
            total: 0,
            pageSize: 15,
        }
    },
    methods: {
        btn_refresh(){
            setTimeout(() =>{
                this.refresh();
            },200);
        },
        changePage(index) {
            let _start = (index -1) * this.pageSize
            let _end = index * this.pageSize
            this.show_list = this.total_list.slice(_start,_end)
        },
        refresh() {
            k8s_namespace_post(get_event_list,{},{},30000).then( (response) => {
                this.total_list = response.data
                this.total = response.data.length
                if(this.total < this.pageSize) {
                    this.show_list = this.total_list
                }else {
                    // 修改改数据之后显示回到第一页的bug，改为停留在当前页
                    let currentPage = this.$refs.page.currentPage
                    let _start = (currentPage-1) * this.pageSize
                    let _end = currentPage * this.pageSize
                    this.show_list = this.total_list.slice(_start,_end)
                }
            })
        }
    },
    mounted: function() {
        setTimeout(() =>{
            this.refresh();
        },200);
    }
}
</script>
<style scoped>

</style>